package com.xqls.biz.impl;

import com.xqls.biz.FluggasDischargeMimRecordBiz;
import com.xqls.service.FluggasDischargeMimRecordService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hdg on 2017/7/17.
 */
@Service
public class FluggasDischargeMimRecordBizImpl implements FluggasDischargeMimRecordBiz {
    private static Logger logger = Logger.getLogger(FluggasDischargeMimRecordBiz.class);

    @Autowired
    FluggasDischargeMimRecordService service;

    @Autowired
    private com.xqls.biz.FluggasBlockAdministrationBiz FluggasBlockAdministrationBiz;

    @Autowired
    private com.xqls.biz.FluggasTableFieldBiz FluggasTableFieldBiz;
    /*分钟查询*/
    @Override
    public CommonResult queryMinFluggasDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();

        try {
            List<String> columns_name = new ArrayList<String>();
            List<Object> columns = new ArrayList<Object>();
            List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns.add("viewId");
            columns.add("recording_time");
            for (String display : displayfiled) {
                if(!display.equals("realdata_no")&&!display.equals("network")&&!display.equals("state")&&!display.equals("power_supply")&&!display.equals("all_state")&&!display.equals("company_name")){
                    columns.add(display);
                }

            }

            List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns_name.add("序号");
            columns_name.add("记录时间");
            for (String fieldalia : fieldalias) {
                if(!fieldalia.equals("编号")&&!fieldalia.equals("排放口")&&!fieldalia.equals("网络")&&!fieldalia.equals("设备门")&&!fieldalia.equals("电源")&&!fieldalia.equals("设备状态")){
                    columns_name.add(fieldalia);
                }

            }

            List<Map<String, Object>> datas = service.selectminByMap(commonPage.pageToMap());

            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                Date time = (Date) datas.get(i).get("recording_time");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(time);
                datas.get(i).put("recording_time",dateString);
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
            }
            commonPage.setPageData(datas);

            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "废气污染记录"));
            showMap.put("companyName",datas.get(0).get("company_name"));
            commonPage.setHtml(VelocityUtil.getHtmlByMap("fluggasRecord/fluggasminDataList.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);

        } catch (Exception e) {
            logger.error("查询废气污染分钟记录异常！", e);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryAll() {
        return service.selectAll();
    }

    /*小时查询*/
    @Override
    public CommonResult queryHourFluggasDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();

        try {
            List<String> columns_name = new ArrayList<String>();
            List<Object> columns = new ArrayList<Object>();
            List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns.add("viewId");
            columns.add("recording_time");
            for (String display : displayfiled) {
                if(!display.equals("realdata_no")&&!display.equals("network")&&!display.equals("state")&&!display.equals("power_supply")&&!display.equals("all_state")&&!display.equals("company_name")){
                    columns.add(display);
                }

            }

            List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns_name.add("序号");
            columns_name.add("记录时间");
            for (String fieldalia : fieldalias) {
                if(!fieldalia.equals("编号")&&!fieldalia.equals("排放口")&&!fieldalia.equals("网络")&&!fieldalia.equals("设备门")&&!fieldalia.equals("电源")&&!fieldalia.equals("设备状态")){
                    columns_name.add(fieldalia);
                }

            }

            List<Map<String, Object>> datas = service.selectHourByMap(commonPage.pageToMap());

            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
            }
            commonPage.setPageData(datas);

            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "废气污染记录"));
            showMap.put("companyName",datas.get(0).get("company_name"));
            commonPage.setHtml(VelocityUtil.getHtmlByMap("fluggasRecord/fluggasHourDataList.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);

        } catch (Exception e) {
            logger.error("查询废气污染小时记录异常！", e);
        }
        return result;
    }

    @Override
    public CommonResult queryDayFluggasDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();

        try {
            List<String> columns_name = new ArrayList<String>();
            List<Object> columns = new ArrayList<Object>();
            List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns.add("viewId");
            columns.add("recording_time");
            for (String display : displayfiled) {
                if(!display.equals("realdata_no")&&!display.equals("network")&&!display.equals("state")&&!display.equals("power_supply")&&!display.equals("all_state")&&!display.equals("company_name")){
                    columns.add(display);
                }

            }

            List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns_name.add("序号");
            columns_name.add("记录时间");
            for (String fieldalia : fieldalias) {
                if(!fieldalia.equals("编号")&&!fieldalia.equals("排放口")&&!fieldalia.equals("网络")&&!fieldalia.equals("设备门")&&!fieldalia.equals("电源")&&!fieldalia.equals("设备状态")){
                    columns_name.add(fieldalia);
                }

            }

            List<Map<String, Object>> datas = service.selectDayByMap(commonPage.pageToMap());

            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                String time =  datas.get(i).get("recording_time").toString();
                String dateString = time.substring(0, time.length() -9);
                datas.get(i).put("recording_time",dateString);
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
            }
            commonPage.setPageData(datas);

            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "废气污染记录"));
            showMap.put("companyName",datas.get(0).get("company_name"));
            commonPage.setHtml(VelocityUtil.getHtmlByMap("fluggasRecord/fluggasdayDataList.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);

        } catch (Exception e) {
            logger.error("查询废气污染天记录异常！", e);
        }
        return result;
    }

    @Override
    public CommonResult queryMothFluggasDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();

        try {
            List<String> columns_name = new ArrayList<String>();
            List<Object> columns = new ArrayList<Object>();
            List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns.add("viewId");
            columns.add("recording_time");
            for (String display : displayfiled) {
                if(!display.equals("realdata_no")&&!display.equals("network")&&!display.equals("state")&&!display.equals("power_supply")&&!display.equals("all_state")){
                    columns.add(display);
                }

            }

            List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns_name.add("序号");
            columns_name.add("记录时间");
            for (String fieldalia : fieldalias) {
                if(!fieldalia.equals("编号")&&!fieldalia.equals("网络")&&!fieldalia.equals("设备门")&&!fieldalia.equals("电源")&&!fieldalia.equals("设备状态")){
                    columns_name.add(fieldalia);
                }

            }

            List<Map<String, Object>> datas = service.selectMonthByMap(commonPage.pageToMap());

            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                String time =  datas.get(i).get("recording_time").toString();
                String dateString = time.substring(0, time.length() -12);
                datas.get(i).put("recording_time",dateString);
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
            }
            commonPage.setPageData(datas);

            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "废气污染记录"));
            showMap.put("companyName",datas.get(0).get("company_name"));
            commonPage.setHtml(VelocityUtil.getHtmlByMap("fluggasRecord/fluggasmothDataList.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);

        } catch (Exception e) {
            logger.error("查询废气污染月记录异常！", e);
        }
        return result;
    }

    @Override
    public CommonResult queryYearFluggasDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();

        try {
            List<String> columns_name = new ArrayList<String>();
            List<Object> columns = new ArrayList<Object>();
            List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns.add("viewId");
            columns.add("recording_time");
            for (String display : displayfiled) {
                if(!display.equals("realdata_no")&&!display.equals("network")&&!display.equals("state")&&!display.equals("power_supply")&&!display.equals("all_state")&&!display.equals("company_name")){
                    columns.add(display);
                }

            }

            List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns_name.add("序号");
            columns_name.add("记录时间");
            for (String fieldalia : fieldalias) {
                if(!fieldalia.equals("编号")&&!fieldalia.equals("网络")&&!fieldalia.equals("设备门")&&!fieldalia.equals("电源")&&!fieldalia.equals("设备状态")&&!fieldalia.equals("排放口")){
                    columns_name.add(fieldalia);
                }

            }

            List<Map<String, Object>> datas = service.selectYearByMap(commonPage.pageToMap());

            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                String time =  datas.get(i).get("recording_time").toString();
                String dateString = time.substring(1, time.length());
                for (int j=1;j<=12;j++){
                   if((""+j).equals(dateString)){
                       dateString=j+"月份";
                   }
                }
                datas.get(i).put("recording_time",dateString);
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
            }
            commonPage.setPageData(datas);

            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "废气污染记录"));
            showMap.put("companyName",datas.get(0).get("company_name"));
            commonPage.setHtml(VelocityUtil.getHtmlByMap("fluggasRecord/fluggasyearDataList.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);

        } catch (Exception e) {
            logger.error("查询废气污染年记录异常！", e);
        }
        return result;
    }
}
